/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.Board;
import Model.Constants;
import Model.Position;
import View.BoardInterface;
import View.ChooseMode;
import View.Creater;
import View.GameInterface;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JOptionPane;

/**
 *
 * @author Long
 */
public class CellListener extends Controller implements ActionListener {

    public CellListener(BoardInterface boardInterface, Board board, GameInterface gameInterface, ChooseMode chooseMode, Creater creater) {
        super(boardInterface, board, gameInterface, chooseMode, creater);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        switch (numberOfModule) {
            case 1:
                module1ActionPerformed(e);
                break;
            case 2:
                module2ActionPerformed(e);
                break;
            case 3:
                module3ActionPerformed(e);
                break;
            default:
                break;
        }
    }

    public void module1ActionPerformed(ActionEvent e) {
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                if (e.getSource() == boardInterface.getArrayOfButton(i, j) && board.getElementOfBoard(i, j) == 0) {
                    isLocked = false;

                    boardInterface.setArrayOfButton(i, j, icon1);
                    boardInterface.setPlayer1Icon(null);
                    boardInterface.setPlayer2Icon(iconTurn);
                    board.updateBoard(i, j, Constants.HUMAN);
                    move++;
                    boardInterface.setTotalMoveLabel("Lượt đi:  " + move);
                    if (CheckWinner.winner(board.getBoardCell(), i, j, 5) == 1) {
                        JOptionPane.showMessageDialog(null, "Xin chúc mừng , bạn đã chiến thắng ! ");
                        board.resetBoard();
                        score1++;
                        boardInterface.setPlayer1Score(score1);
                        boardInterface.setDefaultBoard();
                        move = 0;
                        boardInterface.setTotalMoveLabel("Lượt đi:  " + String.valueOf(move));

                    }
                    if (move == 1) {
                        if (i != 0) {
                            minColumn = i - 1;
                        } else {
                            minColumn = i;
                        }
                        if (i != 19) {
                            maxColumn = i + 1;
                        } else {
                            maxColumn = i;
                        }

                        if (j != 0) {
                            minRow = j - 1;
                        } else {
                            minRow = j;
                        }

                        if (j != 19) {
                            maxRow = j + 1;
                        } else {
                            maxRow = j;
                        }
                    } else {
                        rateData(i, j);
                    }

                    calculateForAI(i, j);
                    boardInterface.setArrayOfButton(finalX, finalY, icon2);
                    boardInterface.setPlayer1Icon(iconTurn);
                    boardInterface.setPlayer2Icon(null);
                    board.updateBoard(finalX, finalY, Constants.COMPUTER);
                    rateData(finalX, finalY);
                    move++;
                    boardInterface.setTotalMoveLabel("Lượt đi:  " + move);
                    if (CheckWinner.winner(board.getBoardCell(), finalX, finalY, 5) == 2) {
                        JOptionPane.showMessageDialog(null, "Bạn đã thua ! ");
                        board.resetBoard();
                        boardInterface.setDefaultBoard();
                        move = 0;
                        score2++;
                        boardInterface.setPlaye2Score(score2);
                        boardInterface.setTotalMoveLabel("Lượt đi: " + String.valueOf(move));
                    }
                }
            }
        }
        if (e.getSource() == boardInterface.getNewGameButton()) {
            board.resetBoard();
            boardInterface.setDefaultBoard();
            move = 0;
            boardInterface.setTotalMoveLabel("Lượt đi: " + String.valueOf(move));
        }
    }
    //action perform module 2  

    public void module2ActionPerformed(ActionEvent e) {

        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {

                if (e.getSource() == boardInterface.getArrayOfButton(i, j) && board.getElementOfBoard(i, j) == 0) {
                    move++;
                    if (move % 2 == 1) {
                        boardInterface.setArrayOfButton(i, j, icon1);
                        board.updateBoard(i, j, 1);
                        boardInterface.setPlayer1Icon(null);
                        boardInterface.setPlayer2Icon(iconTurn);
                        if (CheckWinner.winner(board.getBoardCell(), i, j, 5) == 1) {

                            JOptionPane.showMessageDialog(null, "Người chơi 1 thắng !");
                            score1++;
                            boardInterface.setPlayer1Score(score1);
                            board.resetBoard();
                            boardInterface.setDefaultBoard();
                            boardInterface.setPlayer1Icon(iconTurn);
                            boardInterface.setPlayer2Icon(null);
                            move = 0;
                            boardInterface.setTotalMoveLabel("Lượt đi: " + String.valueOf(move));
                        }
                    } else {
                        boardInterface.setArrayOfButton(i, j, icon2);
                        board.updateBoard(i, j, 2);
                        boardInterface.setPlayer1Icon(iconTurn);
                        boardInterface.setPlayer2Icon(null);
                        if (CheckWinner.winner(board.getBoardCell(), i, j, 5) == 2) {

                            JOptionPane.showMessageDialog(null, "người chơi 2 thắng !");
                            score2++;
                            boardInterface.setPlaye2Score(score2);
                            boardInterface.setPlayer1Icon(iconTurn);
                            boardInterface.setPlayer2Icon(null);
                            board.resetBoard();
                            boardInterface.setDefaultBoard();
                            move = 0;
                            boardInterface.setTotalMoveLabel("Lượt đi: " + String.valueOf(move));
                        }
                    }
                }
            }
            if (e.getSource() == boardInterface.getNewGameButton()) {
                board.resetBoard();
                boardInterface.setDefaultBoard();
                move = 0;
                boardInterface.setPlayer1Icon(iconTurn);
                boardInterface.setPlayer2Icon(null);
            }
        }
    }

    public void module3ActionPerformed(ActionEvent e) {
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 20; j++) {
                if (e.getSource() == boardInterface.getArrayOfButton(i, j) && board.getElementOfBoard(i, j) == 0 && !isLocked) {
                    if (creater.getTypeOfPlayer() == Constants.HOST) {
                        boardInterface.setArrayOfButton(i, j, icon1);
                        board.updateBoard(i, j, Constants.HUMAN);
                        boardInterface.setPlayer2Icon(iconTurn);
                        boardInterface.setPlayer1Icon(null);
                        sendTranfer.sendData(String.valueOf(i) + " " + String.valueOf(j));
                        if (CheckWinner.winner(board.getBoardCell(), i, j, 5) == 1) {
                            boardInterface.setPlayer1Score(boardInterface.getPlayer1Score() + 1);
                            optionTranfer.sendData(Constants.LOSE);
                            JOptionPane.showMessageDialog(null, "Bạn đã dành chiến thắng");
                            board.resetBoard();
                            boardInterface.setDefaultBoard();
                        }
                    } else {
                        boardInterface.setPlayer1Icon(iconTurn);
                        boardInterface.setPlayer2Icon(null);
                        boardInterface.setArrayOfButton(i, j, icon2);
                        board.updateBoard(i, j, Constants.COMPUTER);
                        sendTranfer.sendData(String.valueOf(i) + " " + String.valueOf(j));
                    }
                    isLocked = true;
                }
            }
        }
        if (e.getSource() == boardInterface.getNewGameButton() && creater.getTypeOfPlayer() == Constants.HOST) {
            optionTranfer.sendData(Constants.NEW_GAME);
            System.out.println(Constants.NEW_GAME);
            String rev = optionTranfer.receiveData();
            if (rev.equals(Constants.ACCEPT)) {
                board.resetBoard();
                boardInterface.setDefaultBoard();
            } else {
                JOptionPane.showMessageDialog(null, "Người chơi 2 không đồng ý tiến hành trò chơi mới ! Xin mời tiếp tục ");
            }
        }
    }

    public void rateData(int x, int y) {

        if (x - 1 < minColumn && x - 1 > 0) {
            minColumn = x - 1;
        }
        if (x + 1 > maxColumn && x + 1 <= Constants.numberOfColumns) {
            maxColumn = x + 1;
        }
        if (y - 1 < minRow && y - 1 > 0) {
            minRow = y - 1;
        }
        if (y + 1 > maxRow && y - 1 <= Constants.numberOfRows) {
            maxRow = y + 1;
        }

    }

    public void checkCombo(int comboValue, int object) {
        for (int i = minColumn; i <= maxColumn; i++) {
            for (int j = minRow; j <= maxRow; j++) {
                if (board.getElementOfBoard(i, j) == Constants.NULL) {
                    board.updateBoard(i, j, object);
                    if (CheckWinner.winner(board.getBoardCell(), i, j, comboValue) == object) {
                        finalX = i;
                        finalY = j;
                        isLocked = true;

                    }
                    board.updateBoard(i, j, Constants.NULL);
                }
            }
        }
    }

    public void calculateForAI(int x, int y) {
        if (isLocked == false) {
            checkCombo(5, Constants.HUMAN);
        }
        if (isLocked == false) {
            checkCombo(5, Constants.COMPUTER);
        }
        if (isLocked == false) {
            checkCombo(4, Constants.HUMAN);
        }
        if (isLocked == false) {
            checkCombo(4, Constants.COMPUTER);
        }
        if (isLocked == false) {
            checkCombo(3, Constants.COMPUTER);
        }
        if (isLocked == false) {

            ArrayList<Position> randomPieceArray = new ArrayList();

            for (int i = x - 1; i <= x + 1; i++) {
                for (int j = y - 1; j <= y + 1; j++) {

                    if (i >= 0 && i < 20 && j >= 0 && j < 20 && board.getElementOfBoard(i, j) == 0) {

                        Position p = new Position(i, j);
                        randomPieceArray.add(p);
                    }
                }
            }
            Random rd = new Random();
            Position tmp = randomPieceArray.get(rd.nextInt(randomPieceArray.size()));
            finalX = tmp.getX();
            finalY = tmp.getY();
            isLocked = true;
        }
    }
}
